﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InsuranceCompany.Core.Domain.InsuranceArea
{
    public class InsuranceRisks : InsuranceEntity
    {
        private List<InsuranceRisk> Risks;

        public InsuranceRisks() :base()
        {
            Risks = new List<InsuranceRisk>();
        }
        public InsuranceRisks(InsuranceRisks ir) :base(ir)
        {
            Risks = ir.GetRisks();
        }
        public InsuranceRisks(InsuranceRisk ir) :base()
        {
            Risks = new List<InsuranceRisk>();
            Risks.Add(ir);
        }
        public InsuranceRisks(List<InsuranceRisk> ir)
            : base()
        {
            Risks = new List<InsuranceRisk>(ir);
        }

        public void AddRisk(InsuranceRisk risk) 
        {
            this.resetErrorMessage();
            InsuranceRisk sameRisk = Risks.Find(x => x.Name == risk.Name);
            if (sameRisk == null)
                this.Risks.Add(risk);
            else
            {
                Risks.Remove(sameRisk);
                Risks.Add(risk);
                return;
            }
        }

        public void ReplaceRisks(InsuranceRisks risks)
        {
            Risks.Clear();
            Risks = risks.GetRisks();
        }
        public void AddRisks(List<InsuranceRisk> risks)
        {
            foreach (InsuranceRisk item in risks)
                Risks.Add(item);
            
        }
        public void ReplaceRisks(List<InsuranceRisk> risks)
        {
            Risks.Clear();
            Risks = risks;
        }
        public void DeleteRisk(int riskIndex) { this.Risks.Remove((InsuranceRisk)this.Risks.ToArray().GetValue(riskIndex)); }
        public void DeleteAll() { this.Risks.Clear(); }
        public InsuranceRisk GetRisk(int riskIndex) { return (InsuranceRisk)this.Risks.ToArray().GetValue(riskIndex); }
        public InsuranceRisk GetRisk(string name) { return (InsuranceRisk)this.Risks.Find(x => x.Name == name); }
        public bool ContainsRisk(InsuranceRisk risk) { return this.Risks.Contains(risk); }
        public List<InsuranceRisk> GetRisks() { return Risks; }
        public Decimal CalculateMinSum() 
        {
            Decimal sum = 0;
            foreach (var risk in Risks)
                sum += risk.Sum;
            return sum;
        }

    }
}
